package com.sze.volanmenetrendgyor;

import java.io.IOException;
import java.util.ArrayList;

import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;

import android.content.Context;
import android.database.SQLException;
import android.os.AsyncTask;
import android.util.Log;

public class StationDownloadTask extends AsyncTask<Context, Void, Boolean> {
	Exception ex;
	@Override
	protected Boolean doInBackground(Context... params) {
		try {
			DbConnector mDbConnector = DbConnector.getInstance(params[0]);
			mDbConnector.open();

			ArrayList<StationClass> stationList = StationConnector.getStationFromNet();

			mDbConnector.beginTransaction();

			for (StationClass stationClass : stationList) {
				Log.d("Allomas: ", stationClass.name);
				mDbConnector.replaceStationsData(stationClass);
			}
			mDbConnector.setTransactionSuccessful();
			mDbConnector.endTransaction();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} catch (JsonIOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			ex = e;
			return false;
		}
		
		
		return false;
	}
}
